package org.graduation.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.graduation.model.common.project.Phase;
import org.graduation.model.common.project.Project;
import org.graduation.project.mapper.PhaseMapper;
import org.graduation.project.service.PhaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Service
public class PhaseServiceImpl extends ServiceImpl<PhaseMapper, Phase> implements PhaseService {
    private List<Phase> initialPhaseList;
    @Autowired
    private PhaseMapper phaseMapper;

    @PostConstruct
    public void setInitialPhase() {
        initialPhaseList = new ArrayList<>();
        String[] initialPhaseName = {"准备阶段", "执行阶段", "结束复盘阶段"};
        for (String str : initialPhaseName) {
            Phase phase = new Phase();
            phase.setName(str);
            initialPhaseList.add(phase);
        }
    }

    @Override
    public Integer initialPhase(Project project) {
        for (Phase phase : initialPhaseList) {
            phase.setPId(project.getPId());
            phaseMapper.insert(phase);
        }
        clearInitialPhaseList();
        return initialPhaseList.size();
    }

    /**
     * 仅仅清空initialPhaseList属性内部phase的p_id、phase_id内容
     */
    private void clearInitialPhaseList() {
        for (Phase phase : initialPhaseList) {
            phase.setPhaseId(null);
            phase.setpId(null);
        }
    }

}
